﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTScrapConcrete : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService();


            OperationResult<List<tb_RPT_ScrapConcrete>> result = RPTScrapConcreteServiceObj.GetAllScrapConcrete();

            if (result.operationSuccess)
            {
                gvScrapConcreteList.DataSource = result.obj;
                gvScrapConcreteList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService();

            OperationResult<tb_RPT_ScrapConcrete> resultEdit = RPTScrapConcreteServiceObj.GetScrapConcreteById(id);
            if (resultEdit.operationSuccess == true)
            {
                txtEditWeather.Text = resultEdit.obj.Weather;
                txtEditInputDate.Text = resultEdit.obj.InputDate;
                txtEditProjectName.Text = resultEdit.obj.ProjectName;
                txtEditConstructionUnit.Text = resultEdit.obj.ConstructionUnit;
                txtEditConstructionSite.Text = resultEdit.obj.ConstructionSite;
                txtEditIntensityLevel.Text = resultEdit.obj.IntensityLevel;
                txtEditDevelopmentTime.Text = resultEdit.obj.DevelopmentTime;
                txtEditReason.Text = resultEdit.obj.Reason;
                txtEditQuantity.Text = resultEdit.obj.Quantity;
                txtEditCement.Text = resultEdit.obj.Cement;
                txtEditProductLine.Text = resultEdit.obj.ProductLine;
                txtEditApproachTo.Text = resultEdit.obj.ApproachTo;
                txtEditCarryOn.Text = resultEdit.obj.CarryOn;
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();

            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetDeviceNameById(string DeviceId)
        {
            string returnValue = "";
            returnValue = MaxStudio.Concrete.Component.MasterDateString.GetMasterDataNameById(Int32.Parse(DeviceId));
            return returnValue;
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService();
            tb_RPT_ScrapConcrete instance = new tb_RPT_ScrapConcrete();

            instance.Weather =txtWeather.Text;
            instance.InputDate =txtInputDate.Text;
            instance.ProjectName =txtProjectName.Text;
            instance.ConstructionUnit =txtConstructionUnit.Text;
            instance.ConstructionSite =txtConstructionSite.Text;
            instance.IntensityLevel =txtIntensityLevel.Text;
            instance.DevelopmentTime =txtDevelopmentTime.Text;
            instance.Reason =txtReason.Text;
            instance.Quantity =txtQuantity.Text;
            instance.Cement =txtCement.Text;
            instance.ProductLine =txtProductLine.Text;
            instance.ApproachTo =txtApproachTo.Text;
            instance.CarryOn =txtCarryOn.Text; 

            OperationResult<tb_RPT_ScrapConcrete> result = RPTScrapConcreteServiceObj.CreateScrapConcrete(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService(); 
            tb_RPT_ScrapConcrete entity = new tb_RPT_ScrapConcrete();

            entity.Id = editId;
            entity.Weather = txtEditWeather.Text;
            entity.InputDate = txtEditInputDate.Text;
            entity.ProjectName = txtEditProjectName.Text;
            entity.ConstructionUnit = txtEditConstructionUnit.Text;
            entity.ConstructionSite = txtEditConstructionSite.Text;
            entity.IntensityLevel = txtEditIntensityLevel.Text;
            entity.DevelopmentTime = txtEditDevelopmentTime.Text;
            entity.Reason = txtEditReason.Text;
            entity.Quantity = txtEditQuantity.Text;
            entity.Cement = txtEditCement.Text;
            entity.ProductLine = txtEditProductLine.Text;
            entity.ApproachTo = txtEditApproachTo.Text;
            entity.CarryOn = txtEditCarryOn.Text;
 
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_ScrapConcrete> resultEdit = RPTScrapConcreteServiceObj.ModifyScrapConcrete(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService();

            tb_RPT_ScrapConcrete deleteEntity = new tb_RPT_ScrapConcrete();
            deleteEntity.Id = deleteId;
            RPTScrapConcreteServiceObj.DeleteScrapConcrete(deleteEntity);

            BindGridView();
        }

        protected void gvScrapConcreteList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvScrapConcreteList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvScrapConcreteList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
